//! # 性能配置模块
//! 
//! 定义性能相关的配置结构，包括连接池、线程池和内存配置。

use serde::{Deserialize, Serialize};
use crate::config::{ConnectionPoolConfig, ThreadPoolConfig, MemoryConfig, GcConfig};

/// 性能配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// 连接池配置
    pub connection_pool: ConnectionPoolConfig,
    
    /// 线程池配置
    pub thread_pool: ThreadPoolConfig,
    
    /// 内存配置
    pub memory: MemoryConfig,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            connection_pool: ConnectionPoolConfig::default(),
            thread_pool: ThreadPoolConfig::default(),
            memory: MemoryConfig::default(),
        }
    }
}

/// 连接池配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionPoolConfig {
    /// 最大连接数
    pub max_connections: u32,
    
    /// 最小连接数
    pub min_connections: u32,
    
    /// 连接超时（秒）
    pub connection_timeout_seconds: u64,
    
    /// 空闲超时（秒）
    pub idle_timeout_seconds: u64,
}

impl Default for ConnectionPoolConfig {
    fn default() -> Self {
        Self {
            max_connections: 100,
            min_connections: 10,
            connection_timeout_seconds: 30,
            idle_timeout_seconds: 300,
        }
    }
}

/// 线程池配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThreadPoolConfig {
    /// 核心线程数
    pub core_threads: u32,
    
    /// 最大线程数
    pub max_threads: u32,
    
    /// 队列大小
    pub queue_size: u32,
    
    /// 线程空闲时间（秒）
    pub keep_alive_seconds: u64,
}

impl Default for ThreadPoolConfig {
    fn default() -> Self {
        let cpu_count = num_cpus::get();
        Self {
            core_threads: cpu_count as u32,
            max_threads: (cpu_count * 2) as u32,
            queue_size: 1000,
            keep_alive_seconds: 60,
        }
    }
}

/// 内存配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
    /// 最大堆内存（MB）
    pub max_heap_mb: Option<u64>,
    
    /// GC配置
    pub gc: GcConfig,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            max_heap_mb: None,
            gc: GcConfig::default(),
        }
    }
}

/// GC配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GcConfig {
    /// GC策略
    pub strategy: String,
    
    /// GC阈值
    pub threshold: f64,
}

impl Default for GcConfig {
    fn default() -> Self {
        Self {
            strategy: "default".to_string(),
            threshold: 0.8,
        }
    }
}